package com.bejoy.config.handler;

import cn.hutool.json.JSONUtil;
import com.bejoy.common.AjaxResult;
import com.bejoy.domain.model.TUser;
import com.bejoy.util.JWTUtils;
import com.bejoy.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import java.io.IOException;

public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        TUser tUser = (TUser) authentication.getPrincipal();
        String userString = JSONUtil.toJsonStr(tUser);
        String jwt = JWTUtils.createJWT(userString);
        AjaxResult result = AjaxResult.success("退出成功！");
        String jsonStr = JSONUtil.toJsonStr(result);
        ResponseUtils.write(response,jsonStr);
    }
}
